package com.egg.ylt.activity;

import android.os.Bundle;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.egg.ylt.R;
import com.egg.ylt.Utils.Constants;
import com.egg.ylt.Utils.CustomUtils;
import com.egg.ylt.Utils.StatusBarUtil;
import com.egg.ylt.Utils.StringUtil;
import com.egg.ylt.pojo.LoginEntity;
import com.egg.ylt.presenter.impl.AccountPwdPresenterImpl;
import com.egg.ylt.view.IAccountPassView;
import com.yonyou.framework.library.base.BaseActivity;
import com.yonyou.framework.library.bean.ErrorBean;
import com.yonyou.framework.library.common.utils.AppSharedPreferences;
import com.yonyou.framework.library.common.utils.MD5Util;
import com.yonyou.framework.library.common.utils.ToastUtil;
import com.yonyou.framework.library.eventbus.EventCenter;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.OnClick;

public class ACT_AccountPasswordLogin extends BaseActivity<AccountPwdPresenterImpl> implements IAccountPassView {


    @BindView(R.id.root_layout)
    LinearLayout mRootLayout;
    @BindView(R.id.iv_delete_login)
    ImageView ivDeleteLogin;
    @BindView(R.id.edit_phone_login)
    EditText editPhoneLogin;
    @BindView(R.id.edit_pwd_login)
    EditText editPwdLogin;
    @BindView(R.id.iv_password_show)
    ImageView ivPasswordShow;
    @BindView(R.id.btn_next_login)
    TextView btnNextLogin;
    @BindView(R.id.tv_code_login)
    TextView tvCodeLogin;
    @BindView(R.id.tv_forget_pwd)
    TextView tvForgetPwd;
    @BindView(R.id.login_iv_wx)
    ImageView loginIvWx;
    @BindView(R.id.login_iv_qq)
    ImageView loginIvQq;
    @BindView(R.id.tv_user_protocol)
    TextView tvUserProtocol;
    @BindView(R.id.tv_passwd_error)
    TextView passError;

    private AppSharedPreferences mSp;
    private boolean mPasswordType = true;
    private String companyId = "1";

    @Override
    protected void getBundleExtras(Bundle extras) {

    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.act_account_password_login;
    }

    @Override
    protected View getLoadingTargetView() {
        return null;
    }

    @Override
    protected void initViewsAndEvents() {
        mSp = new AppSharedPreferences(mContext);

        StatusBarUtil.setStatusBarColor(this, R.color.ylt_whit);
        StatusBarUtil.StatusBarLightMode(this);
        CustomUtils.setImmersiveStatusBar(mContext, mRootLayout);
    }

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return false;
    }

    @Override
    protected boolean isBindEventBusHere() {
        return false;
    }

    @Override
    protected boolean toggleOverridePendingTransition() {
        return false;
    }

    @Override
    protected TransitionMode getOverridePendingTransitionMode() {
        return null;
    }

    @Override
    protected boolean isApplyKitKatTranslucency() {
        return false;
    }

    @Override
    public void showBusinessError(ErrorBean error) {

    }

    @OnClick({R.id.iv_delete_login, R.id.iv_password_show, R.id.btn_next_login, R.id.tv_code_login,
            R.id.tv_forget_pwd, R.id.tv_user_protocol})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_delete_login://返回键
                finish();
                break;
            case R.id.iv_password_show://显示和隐藏密码
                if (mPasswordType) {
                    editPwdLogin.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    ivPasswordShow.setImageResource(R.mipmap.ic_login_pwd_hide);
                    mPasswordType = false;
                } else {
                    editPwdLogin.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    ivPasswordShow.setImageResource(R.mipmap.ic_login_pwd_show);
                    mPasswordType = true;
                }
                //设置光标在内容的末尾
                editPwdLogin.setSelection(editPwdLogin.getText().toString().length());
                break;
            case R.id.btn_next_login://登录
                goLogin();
                break;
            case R.id.tv_code_login://去手机验证码登录
                finish();
                break;
            case R.id.tv_forget_pwd://忘记密码
                readyGo(ACT_FindPassword.class);
                break;
            case R.id.tv_user_protocol://用户协议
                readyGo(ACT_UserProtocol.class);
                break;

        }
    }

    /**
     * 去登录
     */
    private void goLogin() {
        String phone = editPhoneLogin.getText().toString().trim();
        String pwd = editPwdLogin.getText().toString().trim();
        if (!StringUtil.isPhone(phone)) {
            ToastUtil.makeText(mContext, "手机号不正确", false);
            return;
        }
        if (pwd.length() < 6 && StringUtil.isEmpty(pwd)) {
            ToastUtil.makeText(mContext, "请输入密码为6-12位", false);
            return;
        }
        mPresenter.goToLogin(phone, MD5Util.string2MD5(pwd), Constants.COMPANYID);
    }

    @Override
    public void onLoginSucceed(LoginEntity entity) {
        //存储token到本地
        if (entity == null && StringUtil.isEmpty(entity.getId())) {
            return;
        }
        //把token存入本地
        mSp.putString(Constants.USER_TOKEN, entity.getToken());
        //用户id
        mSp.putString(Constants.USER_ID, entity.getId());
        //钱包金额
        mSp.putString(Constants.TOTAL_AMOUNT, entity.getTotalAmount());
        //公司id
        mSp.putString(Constants.COMPANY_ID, entity.getCompanyId());
        //手机号
        mSp.putString(Constants.USER_PHONE, entity.getPhone());
        //用户名
        mSp.putString(Constants.USER_NAME, entity.getName());
        //用户头像uri
        mSp.putString(Constants.HEAD_IMAGE, entity.getHeadImage());
        //用户积分
        mSp.putString(Constants.USER_FLOW, entity.getFlow());
        //初始化公共参数
        Constants.TOKEN = entity.getToken();
        Constants.COMPANYID = entity.getCompanyId();
        Constants.USERID = entity.getId();
        //跳转到首页
        readyGoThenKill(ACT_HomePage.class);

        // 登录成功发送首页优惠券请求
        EventBus.getDefault().post(new EventCenter(Constants.REQUEST_HOMEPAGE_COUPON));
    }

    @Override
    public void setErrorData(String msg) {
        passError.setVisibility(View.VISIBLE);
        passError.setText(msg);
    }

}
